//
//  BottomView.m
//  NewPro
//
//  Created by RYX on 2018/12/19.
//  Copyright © 2018 何荧红. All rights reserved.
//

#import "BottomView.h"

@interface BottomView ()<UITextViewDelegate>

@property (nonatomic,weak) UILabel *placeHoderLa;

@end

@implementation BottomView

- (instancetype)init{
    
    if (self = [super init]) {
        
        self.backgroundColor = [UIColor colorWithHexString:@"#F6F6F8"];
        [self setUI];
    }
    
    return self;
}

- (void)setUI{
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:line];
    
    UIButton *btn = [[UIButton alloc] init];
    [btn setTitle:@"发送" forState:UIControlStateNormal];
    btn.layer.cornerRadius = 5;
    btn.backgroundColor = BLUECOLOR;
    [btn addTarget:self action:@selector(onBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
    
    UIView *tfView = [[UIView alloc] init];
    tfView.layer.borderWidth = 0.1;
    tfView.layer.cornerRadius = 5;
    tfView.backgroundColor = [UIColor colorWithHexString:@"#FDFDFD"];
    [self addSubview:tfView];
    
    UITextView *tf = [[UITextView alloc] init];
    tf.font = [UIFont systemFontOfSize:17];
    tf.delegate = self;
    tf.returnKeyType = UIReturnKeySend;
    [tfView addSubview:tf];
    self.tf = tf;
    
    UILabel *placeHoderLa = [[UILabel alloc] init];
    self.placeHoderLa = placeHoderLa;
    placeHoderLa.text = @"请输入..";
    placeHoderLa.textColor = [UIColor colorWithHexString:@"#CCCCCC"];
    [tfView addSubview:placeHoderLa];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_offset(0);
        make.left.right.equalTo(self);
        make.height.mas_offset(0.2);
    }];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_offset(8);
        make.right.mas_offset(-14);
        make.width.mas_offset(60);
        make.height.mas_offset(35);
    }];
    
    [tfView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_offset(14);
        make.right.equalTo(btn.mas_left).offset(-8);
        make.centerY.equalTo(btn);
        make.height.mas_offset(35);
    }];
    
    [tf mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_offset(8);
        make.top.bottom.equalTo(tfView);
        make.centerY.equalTo(btn);
        make.right.mas_offset(-8);
    }];
    
    [placeHoderLa mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.bottom.equalTo(tf);
        make.left.equalTo(tf.mas_left).offset(5);
    }];
}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车，即按下return
        //在这里做你响应return键的代码
        [self.tf resignFirstResponder];
        if (self.delegate && [self.delegate respondsToSelector:@selector(onKeyBoardReturnBtnWithMsg:)]) {
            
            [self.delegate onKeyBoardReturnBtnWithMsg:self.tf.text];
        }
        
        return NO; //这里返回NO，就代表return键值失效，即页面上按下return，不会出现换行，如果为yes，则输入页面会换行
    }
    
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView{
    
    if (textView.text.length > 0) {
        
        self.placeHoderLa.hidden = YES;
    }else{
        
        self.placeHoderLa.hidden = NO;
    }
}

- (void)onBtn:(UIButton *)sender{
    
    if (self.tf.text.length <= 0) {
        
        return;
    }
    
    self.placeHoderLa.hidden = NO;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(onKeyBoardReturnBtnWithMsg:)]) {
        
        [self.delegate onKeyBoardReturnBtnWithMsg:self.tf.text];
    }
}


@end
